package com.eastfair.common.controller.authcode;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.common.dto.AuthCodeDTO;
import com.eastfair.common.entity.AuthCode;
import com.eastfair.common.dto.AuthCodeSaveDTO;
import com.eastfair.common.dto.AuthCodeUpdateDTO;
import com.eastfair.common.dto.AuthCodePageQuery;
import com.eastfair.common.service.AuthCodeService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.common.vo.AuthCodeVO;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * 服务商对外授权码
 * </p>
 *
 * @author dqq
 * @date 2023-02-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/authCode")
@Api(value = "AuthCode", tags = "服务商对外授权码")
@PreAuth(replace = "common:authCode:")
public class AuthCodeController extends SuperController<AuthCodeService, Long, AuthCode, AuthCodePageQuery, AuthCodeSaveDTO, AuthCodeUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private AuthCodeService authCodeService;

    @Override
    public void handlerResult(IPage<AuthCode> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<AuthCode> authCodeList = list
                .stream()
                .map((map) -> AuthCode.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(authCodeList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<AuthCodePageQuery> params) {
        AuthCodePageQuery pageQuery = params.getModel();
    }

    /**
     * 授用用户ID查询授权码信息.
     *
     * @param authCodeDTO the auth code dto
     * @return the by auth user id
     */
    @PostMapping("/get/authUserId")
    public R<AuthCodeVO> getByAuthUserId(@RequestBody AuthCodeDTO authCodeDTO) {
        log.info("getByAuthUserId - 授用用户ID查询授权码信息, authCodeDTO={}", authCodeDTO);
        return authCodeService.getVOByAuthUserId(authCodeDTO);
    }

    /**
     * 分页查询授权码VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询授权码VO")
    @PostMapping("/query/authCode")
    public R<Page<AuthCodeVO>> queryAuthCode(@RequestBody PageParams<AuthCodeDTO> pageParams) {
        log.info("queryAuthCode - 分页查询授权码VO, pageParams={}", pageParams);
        return R.successDef(authCodeService.listAuthCodeVO(pageParams));
    }

    /**
     * ID 查询授权码.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<AuthCodeVO> getById(@PathVariable("id") Long id) {
        log.info("getById - ID 查询授权码, id={}", id);
        return R.successDef(authCodeService.getVOById(id));
    }

    /**
     * 保存授权码.
     *
     * @param authCodeDTO the auth code dto
     * @return the r
     */
    @ApiOperation("保存授权码")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody AuthCodeDTO authCodeDTO) {
        log.info("save - 保存授权码, authCodeDTO={}", authCodeDTO);
        return R.successDef(authCodeService.saveAuthCode(authCodeDTO));
    }

    /**
     * 更新授权码.
     *
     * @param authCodeDTO the auth code dto
     * @return the r
     */
    @ApiOperation("更新授权码")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody AuthCodeDTO authCodeDTO) {
        log.info("update - 更新授权码, authCodeDTO={}", authCodeDTO);
        return R.successDef(authCodeService.updateAuthCode(authCodeDTO));
    }

    /**
     * 移除授权码.
     *
     * @param ids the ids
     * @return the r
     */
    @ApiOperation("移除授权码")
    @PostMapping("/delete")
    public R<Boolean> removeByIds(@RequestBody List<Long> ids) {
        log.info("removeByIds - 移除授权码, ids={}", ids);
        return R.successDef(authCodeService.removeAuthCodeByIds(ids));
    }
}
